package com.elite;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.xml.rpc.holders.StringHolder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.yasms.YASMS;




public class DBSMS implements Runnable{
	private static final Log log=LogFactory.getLog(DBSMS.class);
	public static com.elite.FtpBean ftpbean;
	public static String times;
	private boolean flag = true; 
	private static int Hz =1; 
	private static String sql =" select * from Documentout where DOCTYPE=3 and ((Status=0) or (Status in ('4','5','6','7','8') and TrialCount<TrialCountLimit)) and expectedstartdate<=sysdate "; 
	public static String username;
	public static String password;
	private static String LastTryDateTime="";
	private static String FinishDateTime="";
	public static String IP;
	private static int xh_int=0;
	private static SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
	public static String getIP() {
		return IP;
	}
	public static void setIP(String ip) {
		IP = ip;
	}
	public static com.elite.FtpBean getFtpbean() {
		return ftpbean;
	}
	public static void setFtpbean(com.elite.FtpBean ftpbean) {
		DBSMS.ftpbean = ftpbean;
	}
	public static String getTimes() {
		return times;
	}
	public static void setTimes(String times) {
		DBSMS.times = times;
	}
	public static String getUsername() {
		return username;
	}
	public static void setUsername(String username) {
		DBSMS.username = username;
	}
	public static String getPassword() {
		return password;
	}
	public static void setPassword(String password) {
		DBSMS.password = password;
	}

	private String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw = new StringWriter();  
            e.printStackTrace(new PrintWriter(sw, true));  
            str = sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}
	public void run(){
		log.info("---------------------永安短信接口----------------------------");
		while(flag){
			try{
				log.info("sss:"+times);
				Hz=Integer.parseInt(times);
			}catch(Exception e){
				log.info("错误:"+e);
				log.info("因为获取运行频率出错，改为默认频率1分钟运行");
			}
			log.info("扫描documentout表中未处理的记录："+sql);
			List list=null;
			try{
				list=ftpbean.chaxun(sql);
			}catch(Exception e){
				log.error("扫描documentout表_error:"+getERRO(e));
			}
			if(list!=null){
				for(int i=0;i<list.size();i++){
					Map map=(Map)list.get(i); 
					String DocumentOut_GUID=Util.filterForObject(map.get("DocumentOut_GUID"));
					log.debug("DocumentOut_GUID:"+DocumentOut_GUID);
					String Organization=Util.filterForObject(map.get("Organization"));
					log.debug("Organization:"+Organization);
					String ServiceType=Util.filterForObject(map.get("ServiceType"));
					log.debug("ServiceType:"+ServiceType);
					String DOCUMENTTO=Util.filterForObject(map.get("DOCUMENTTO"));
					log.debug("DOCUMENTTO:"+DOCUMENTTO);
					String COMMENTS=Util.filterForObject(map.get("COMMENTS"));
					log.debug("COMMENTS:"+COMMENTS);
					String OrgCode=Util.filterForObject(map.get("OrgCode"));
					log.debug("OrgCode:"+OrgCode);
					String STRINGFIELD1=Util.filterForObject(map.get("STRINGFIELD1"));
					log.debug("STRINGFIELD1:"+STRINGFIELD1);
					String update_sql="";				
					try{
						YASMS yASMS=new YASMS();
						yASMS.SendSms(IP,DocumentOut_GUID,Organization, ServiceType,DOCUMENTTO, COMMENTS, OrgCode, username, password,STRINGFIELD1);
						update_sql=" update DocumentOut set Status=1,TrialCount=TrialCount+1,LastTryDateTime=to_date('"+LastTryDateTime+"','yyyy-mm-dd hh24:mi:ss'),FinishDateTime=to_date('"+FinishDateTime+"','yyyy-mm-dd hh24:mi:ss'),ModifiedDate=sysdate where DocumentOut_GUID='"+DocumentOut_GUID+"'";
					}catch(Exception e){
						update_sql=" update DocumentOut set FailCode=0,FailDescription='wrong',Status=5,TrialCount=TrialCount+1,LastTryDateTime=to_date('"+LastTryDateTime+"','yyyy-mm-dd hh24:mi:ss'),FinishDateTime=to_date('"+FinishDateTime+"','yyyy-mm-dd hh24:mi:ss'),ModifiedDate=sysdate where DocumentOut_GUID='"+DocumentOut_GUID+"'";
					}
					log.info("update_sql:"+update_sql);
					try{
						int a=ftpbean.update(update_sql);
						log.info("是否更新DocumentOut成功:"+a);
					}catch(Exception e){
						log.info("更新DocumentOut出错:"+e);
					}
				}
				
			}else{
				log.info("没有需要发送的短信");
			}
                
			try{
				Thread.sleep(Hz*1000);
			}catch(Exception e){
				log.error("定时错误:"+getERRO(e));
			}
		}
	}

	

}





















